package cn.com.surker.report;

import cn.com.surker.util.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @Author lucky
 * @create 2023年10月19 10:32
 * @desc
 */
public class ExcelStyleUtils {

	/**
	 * 返回单元格样式
	 *
	 * @param wb
	 * @param isVerticalCenter  是否垂直居中
	 * @param isSolidForeground 是否固定
	 * @param isAlignCenter     文字居中
	 * @param isTop             上边框
	 * @param isBottom          下边框
	 * @param isLeft            左边框
	 * @param isRight           右边框
	 * @return
	 */
	public static XSSFCellStyle cellStyle(XSSFWorkbook wb, Boolean isVerticalCenter, Boolean isSolidForeground, Boolean isAlignCenter,
										  Boolean isTop, Boolean isBottom, Boolean isLeft, Boolean isRight, Short color) {
		XSSFCellStyle valueStyle = wb.createCellStyle();
		if (!StringUtils.isNull(isVerticalCenter) && isVerticalCenter)
			valueStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
		if (!StringUtils.isNull(isSolidForeground) && isSolidForeground)
			valueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		if (!StringUtils.isNull(isAlignCenter) && isAlignCenter)
			valueStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
		if (!StringUtils.isNull(isBottom) && isBottom)
			valueStyle.setBorderBottom(BorderStyle.THIN); //下边框
		if (!StringUtils.isNull(isLeft) && isLeft)
			valueStyle.setBorderLeft(BorderStyle.THIN);//左边框
		if (!StringUtils.isNull(isTop) && isTop)
			valueStyle.setBorderTop(BorderStyle.THIN);//上边框
		if (!StringUtils.isNull(isRight) && isRight)
			valueStyle.setBorderRight(BorderStyle.THIN);//右边框
		if (StringUtils.isNull(color))
			color = IndexedColors.WHITE.index;
		valueStyle.setFillForegroundColor(color);
		return valueStyle;
	}

	/**
	 * 返回单元格样式
	 *
	 * @param wb
	 * @param isVerticalCenter  是否垂直居中
	 * @param isSolidForeground 是否固定
	 * @param isAlignCenter     文字居中
	 * @param isTop             上边框
	 * @param isBottom          下边框
	 * @param isLeft            左边框
	 * @param isRight           右边框
	 * @return
	 */
	public static XSSFCellStyle cellStyle(XSSFWorkbook wb, Boolean isVerticalCenter, Boolean isSolidForeground, Boolean isAlignCenter,
										  Boolean isTop, Boolean isBottom, Boolean isLeft, Boolean isRight, Short topColor,
										  Short bottomColor, Short leftColor, Short rightColor, Short color) {
		XSSFCellStyle valueStyle = wb.createCellStyle();
		if (!StringUtils.isNull(isVerticalCenter) && isVerticalCenter)
			valueStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
		if (!StringUtils.isNull(isSolidForeground) && isSolidForeground)
			valueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		if (!StringUtils.isNull(isAlignCenter) && isAlignCenter)
			valueStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
		if (!StringUtils.isNull(isBottom) && isBottom)
			valueStyle.setBorderBottom(BorderStyle.THIN); //下边框
		if (!StringUtils.isNull(isLeft) && isLeft)
			valueStyle.setBorderLeft(BorderStyle.THIN);//左边框
		if (!StringUtils.isNull(isTop) && isTop)
			valueStyle.setBorderTop(BorderStyle.THIN);//上边框
		if (!StringUtils.isNull(isRight) && isRight)
			valueStyle.setBorderRight(BorderStyle.THIN);//右边框
		if (StringUtils.isNull(color))
			color = IndexedColors.WHITE.index;
		if (!StringUtils.isNull(bottomColor))
			valueStyle.setBottomBorderColor(bottomColor); //下边框颜色
		if (!StringUtils.isNull(leftColor))
			valueStyle.setLeftBorderColor(leftColor);//左边框
		if (!StringUtils.isNull(topColor))
			valueStyle.setTopBorderColor(topColor);//上边框
		if (!StringUtils.isNull(rightColor))
			valueStyle.setRightBorderColor(rightColor);//右边框

		valueStyle.setFillForegroundColor(color);
		return valueStyle;
	}

	public static XSSFFont hssfFont(XSSFWorkbook wb, Integer fontSize, String fontName, Short color, Boolean bold) {
		XSSFFont font = wb.createFont();
		if (StringUtils.isNull(fontSize))
			fontSize = 10;
		font.setFontHeightInPoints(fontSize.shortValue());//设置字体大小
		if (StringUtils.isNull(fontName))
			fontName = "宋体";
		font.setFontName(fontName);
		if (StringUtils.isNull(color))
			color = IndexedColors.BLACK.index;
		if (!StringUtils.isNull(bold))
			font.setBold(bold);
		font.setColor(color);

		return font;
	}
	/**
	 * 返回单元格样式
	 * @param wb
	 * @param alignment 是否垂直居中
	 * @param isSolidForeground 是否固定
	 * @param isAlignCenter 文字居中
	 * @param isTop 上边框
	 * @param isBottom 下边框
	 * @param isLeft 左边框
	 * @param isRight 右边框
	 * @return
	 */
	public static XSSFCellStyle cellStyle(XSSFWorkbook wb, Boolean isVerticalCenter, Short alignment, Boolean isSolidForeground, Boolean isAlignCenter,
										  Boolean isTop, Boolean isBottom, Boolean isLeft, Boolean isRight, Short color) {
		XSSFCellStyle valueStyle = wb.createCellStyle();
		if (!StringUtils.isNull(isVerticalCenter) && isVerticalCenter)
			valueStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
//        if (!StringUtils.isNull(alignment))
//            valueStyle.setAlignment(alignment);//垂直居中
		if (!StringUtils.isNull(isSolidForeground) && isSolidForeground)
			valueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		if (!StringUtils.isNull(isAlignCenter) && isAlignCenter)
			valueStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
		if (!StringUtils.isNull(isBottom) && isBottom)
			valueStyle.setBorderBottom(BorderStyle.THIN); //下边框
		if (!StringUtils.isNull(isLeft) && isLeft)
			valueStyle.setBorderLeft(BorderStyle.THIN);//左边框
		if (!StringUtils.isNull(isTop) && isTop)
			valueStyle.setBorderTop(BorderStyle.THIN);//上边框
		if (!StringUtils.isNull(isRight) && isRight)
			valueStyle.setBorderRight(BorderStyle.THIN);//右边框
		if (StringUtils.isNull(color))
			color = IndexedColors.WHITE.index;
		valueStyle.setFillForegroundColor(color);
		return valueStyle;
	}
}
